package io.renren.modules.wyzsphone.commWithWCF;

import com.google.gson.Gson;
import io.renren.modules.wyzsphone.utils.Utils;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.Map;


public class CommWithWCF {
	private static final String NAMESPACE = "http://tempuri.org/";
	private static String URL = "http://20041.zzlysbd.com:18701/Mobile.svc/";
	private Gson _gson = new Gson();
	private String Message;
	public String GetMessage(){
		return this.Message;
	}
	
    //�����ӿ� �õ���������
    public XModeRtn Code(XModeSnd mp){

    	String METHOD_NAME = "Code";
    	String SOAP_ACTION = "http://tempuri.org/IMobileApi/Code";
    	String Value ="{'msg':'提交失败'}";
    	String ReachURL="";
    	XModeRtn 	ValueRtn = _gson.fromJson(Value, XModeRtn.class);
    	try{
    		//GloabFile.Url="http://20004.zzlysbd.com:18701/Mobile.svc/";
//    		if(GloabFile.Url.contains("http")){
//    			URL=GloabFile.Url;
//    			ReachURL=URL.replace("http://", "");
//    			ReachURL=ReachURL.replace(":18701/XMode.svc/", "");
//    			ReachURL=ReachURL.replace(":18701/Mobile.svc/", "");
//    		}
//    		else if(GloabFile.Url.contains(".")){
//    			URL=String.format("http://%s:18701/XMode.svc/", GloabFile.Url);
//    			ReachURL=GloabFile.Url;
//    		}
//    		else{
//    			URL=String.format("http://%s.zzlysbd.com:18701/XMode.svc/", GloabFile.Url);
//    			ReachURL=URL.replace("http://", "");
//    			ReachURL=ReachURL.replace(":18701/XMode.svc/", "");
//    			ReachURL=ReachURL.replace(":18701/Mobile.svc/", "");
//    		}
//
//			if(!Common.isReachable(ReachURL)){
//				Message="���������쳣";
//				ValueRtn=new XModeRtn();
//				ValueRtn.getData().setData("state", "error");
//				ValueRtn.getData().setData("message",Message);
//				ValueRtn.setMessage(Message);
//				return ValueRtn;
//			}
    		
    		HttpTransportSE ht = new HttpTransportSE(mp.getURL());
    		ht.debug = true;

    		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
    		SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

    		String mpString = _gson.toJson(mp);
    		
    		mpString = CommonEncryptUtil.aesEncrypt(mpString,"TUfdU9FdGfSc5IdP");
			request.addProperty("encrypt", mpString);

    		envelope.bodyOut = request;
    		envelope.dotNet= true;
    		envelope.setOutputSoapObject(request);
    		
    		ht.call(SOAP_ACTION, envelope);
    		
	        if(envelope.getResponse()!= null){
	        	String res = envelope.getResponse().toString();
	        	System.out.println(res);
	        	Value=CommonEncryptUtil.aesDecrypt(res, "TUfdU9FdGfSc5IdP");
	        	ValueRtn = _gson.fromJson(Value, XModeRtn.class);
	        	ValueRtn.setMessage(Value);
	        } else{
	        	Message = "nothing";
	        }

    	}catch(SoapFault e){
    		e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
    	}catch(IOException e){
    		e.printStackTrace();
			ValueRtn.setMessage(e.toString());
    	}catch(XmlPullParserException e){
	    		e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
    	}catch(Exception e){
    		e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
    	}
    	
   	return ValueRtn;
 }


	//�����ӿ� �õ���������
	public String Code2(XModeSnd mp){

		String METHOD_NAME = "Code";
		String SOAP_ACTION = "http://tempuri.org/IMobileApi/Code";
		String Value ="";
		String ReachURL="";
		XModeRtn ValueRtn=null;
		try{
			//GloabFile.Url="http://20004.zzlysbd.com:18701/Mobile.svc/";
//    		if(GloabFile.Url.contains("http")){
//    			URL=GloabFile.Url;
//    			ReachURL=URL.replace("http://", "");
//    			ReachURL=ReachURL.replace(":18701/XMode.svc/", "");
//    			ReachURL=ReachURL.replace(":18701/Mobile.svc/", "");
//    		}
//    		else if(GloabFile.Url.contains(".")){
//    			URL=String.format("http://%s:18701/XMode.svc/", GloabFile.Url);
//    			ReachURL=GloabFile.Url;
//    		}
//    		else{
//    			URL=String.format("http://%s.zzlysbd.com:18701/XMode.svc/", GloabFile.Url);
//    			ReachURL=URL.replace("http://", "");
//    			ReachURL=ReachURL.replace(":18701/XMode.svc/", "");
//    			ReachURL=ReachURL.replace(":18701/Mobile.svc/", "");
//    		}
//
//			if(!Common.isReachable(ReachURL)){
//				Message="���������쳣";
//				ValueRtn=new XModeRtn();
//				ValueRtn.getData().setData("state", "error");
//				ValueRtn.getData().setData("message",Message);
//				ValueRtn.setMessage(Message);
//				return ValueRtn;
//			}

			HttpTransportSE ht = new HttpTransportSE(mp.getURL());
			ht.debug = true;

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
			SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

			String mpString = _gson.toJson(mp);

			mpString = CommonEncryptUtil.aesEncrypt(mpString,"TUfdU9FdGfSc5IdP");
			request.addProperty("encrypt", mpString);

			envelope.bodyOut = request;
			envelope.dotNet= true;
			envelope.setOutputSoapObject(request);

			ht.call(SOAP_ACTION, envelope);

			if(envelope.getResponse()!= null){
				String res = envelope.getResponse().toString();
				System.out.println(res);
				Value=CommonEncryptUtil.aesDecrypt(res, "TUfdU9FdGfSc5IdP");
				ValueRtn = _gson.fromJson(Value, XModeRtn.class);
			} else{
				Message = "nothing";
			}

		}catch(SoapFault e){
			e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
		}catch(IOException e){
			e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
		}catch(XmlPullParserException e){
			e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
		}catch(Exception e){
			e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
		}

		return Value;
	}

	//�����ӿ� �õ���������
	public String Code3(XModeSnd mp){

		String METHOD_NAME = "Code";
		String SOAP_ACTION = "http://tempuri.org/IMobileApi/Code";
		String Value ="";
		String ReachURL="";
		XModeRtn ValueRtn=null;
		try{
			//GloabFile.Url="http://20004.zzlysbd.com:18701/Mobile.svc/";
//    		if(GloabFile.Url.contains("http")){
//    			URL=GloabFile.Url;
//    			ReachURL=URL.replace("http://", "");
//    			ReachURL=ReachURL.replace(":18701/XMode.svc/", "");
//    			ReachURL=ReachURL.replace(":18701/Mobile.svc/", "");
//    		}
//    		else if(GloabFile.Url.contains(".")){
//    			URL=String.format("http://%s:18701/XMode.svc/", GloabFile.Url);
//    			ReachURL=GloabFile.Url;
//    		}
//    		else{
//    			URL=String.format("http://%s.zzlysbd.com:18701/XMode.svc/", GloabFile.Url);
//    			ReachURL=URL.replace("http://", "");
//    			ReachURL=ReachURL.replace(":18701/XMode.svc/", "");
//    			ReachURL=ReachURL.replace(":18701/Mobile.svc/", "");
//    		}
//
//			if(!Common.isReachable(ReachURL)){
//				Message="���������쳣";
//				ValueRtn=new XModeRtn();
//				ValueRtn.getData().setData("state", "error");
//				ValueRtn.getData().setData("message",Message);
//				ValueRtn.setMessage(Message);
//				return ValueRtn;
//			}

			HttpTransportSE ht = new HttpTransportSE(mp.getURL());
			ht.debug = true;

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
			SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);

			String mpString = _gson.toJson(mp);

			mpString = CommonEncryptUtil.aesEncrypt(mpString,"TUfdU9FdGfSc5IdP");
			//request.addProperty("encrypt", mpString);
			HttpConnectionUtil.postRequset(mp.getURL(),mpString);
			envelope.bodyOut = request;
			envelope.dotNet= true;
			envelope.setOutputSoapObject(request);
			//ht.call(SOAP_ACTION, envelope);

			if(envelope.getResponse()!= null){
				String res = envelope.getResponse().toString();
				System.out.println(res);
				Value=CommonEncryptUtil.aesDecrypt(res, "TUfdU9FdGfSc5IdP");
				ValueRtn = _gson.fromJson(Value, XModeRtn.class);
			} else{
				Message = "nothing";
			}

		}catch(SoapFault e){
			e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
		}catch(IOException e){
			e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
		}catch(XmlPullParserException e){
			e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
		}catch(Exception e){
			e.printStackTrace();
			ValueRtn.setMessage(e.getMessage());
		}

		return Value;
	}
    
}
